import 'package:flutter/material.dart';

// 未读数量红点
class XinDongUnreadCountWidget extends StatelessWidget {
  final int? count; // 数量
  final int maxCount; // 最大数量，默认0无限制，非0则超过此值就显示99+之类的
  final double height;
  final double fontSize;
  final bool onlyDot;
  final Color color;

  const XinDongUnreadCountWidget(
      {Key? key,
      this.count,
      this.height = 18.0,
      this.fontSize = 12.0,
      this.maxCount = 0,
      this.onlyDot = false,
      this.color = Colors.red})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (count == null || count == 0) {
      return const SizedBox();
    }
    if (onlyDot) {
      return Container(
        width: height,
        height: height,
        decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(height)),
      );
    }

    return Container(
      height: height,
      alignment: Alignment.center,
      padding: const EdgeInsets.only(left: 5, right: 5),
      constraints: BoxConstraints(minWidth: height),
      decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(height)),
      child: Text(
        getCountText(),
        textAlign: TextAlign.center,
        style: TextStyle(fontSize: fontSize, fontWeight: FontWeight.normal, color: Colors.white, height: 1.2),
      ),
    );
  }

  String getCountText() {
    if (maxCount == 0) {
      return '$count';
    } else {
      if (count! <= maxCount) {
        return '$count';
      } else {
        return "$maxCount+";
      }
    }
  }
}
